﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Reflection;

using log4net;
using log4net.Config;



namespace GenerateDbApp
{



    public partial class Main : Form
    {
        private static readonly ILog Log4net = LogManager.GetLogger(typeof(Program));
        private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);

        public Main()
        {
            InitializeComponent();
            
            BackgroundWorker.WorkerReportsProgress = true;
            BackgroundWorker.WorkerSupportsCancellation = true;
            Log4net.Info("Entering application.");
            Log4net.Error("First error.");
            //string Text = ConfigurationManager.AppSettings["PointsInGraph"].ToString();
        }
        private void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object[] { control, propertyName, propertyValue });
            }
            else
            {
                control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
            }
        }
        private void Button_Start_Click(object sender, EventArgs e)
        {
            BackgroundWorker.RunWorkerAsync();
        }
        private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker.ReportProgress(0);
            SetControlPropertyThreadSafe(Button_Start, "Enabled", false);

            SetControlPropertyThreadSafe(Label_Process, "Text", "Prepearing...");
            FileXML.FileXMLMethod(BackgroundWorker).ReadXML(Edit_XMLAdress.Text);
            SetControlPropertyThreadSafe(Label_Process, "Text", "Adding common data...");
            FileXML.FileXMLMethod(BackgroundWorker).AddToDbCommon();
            SetControlPropertyThreadSafe(Label_Process, "Text", "Adding cache data...");
            FileXML.FileXMLMethod(BackgroundWorker).AddToDbCache();
            SetControlPropertyThreadSafe(Label_Process, "Text", "Finished!");

            SetControlPropertyThreadSafe(Button_Start, "Enabled", true);
        }
        private void BackgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            ProgressBar.Value = e.ProgressPercentage;
        }
        private void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Finished!");
        }
    }



}